<template>
  <div class="account-list">
    <page-header title="账户列表">
      <page-search :config="searchConfig" :model="searchModel" @search="getData">
        <el-button @click="createAccount()">添加账号</el-button>
      </page-search>
    </page-header>
    <el-table :data="tableData" :max-height="clientHeight - 250">
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :min-width="column.minWidth"
        :label="column.label">
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="120px"
      >
        <template slot-scope="{ row }">
          <action-text @click="modify(row)">修改</action-text>
          <action-text @click="deleteData(row)" type="danger">禁用</action-text>
          <action-text @click="modifyPassword(row)">修改密码</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <form-dialog
      ref="formDialog"
      :initFormData="formData"
      :isModify="isModify"
      :isModifyPassword="isModifyPassword"
      @confirm="getData"
      :isCreate="isCreate">
    </form-dialog>
  </div>
</template>
<script>
import { ROLE_TYPE_OPTIONS, ROLE_TYPE } from '@/utils/constants';
import { getAdminUserList, deleleAdminUser } from '@/api/account';
import paginationMixin from '@/mixins/pagination';
import formDialog from './dialog';

const initFormData = {
  account: '',
  name: '',
  pwd: '',
  rePwd: '',
};

export default {
  data() {
    return {
      searchConfig: [
        {
          type: 'select',
          label: '角色',
          prop: 'roleType',
          options: ROLE_TYPE_OPTIONS,
        },
        {
          type: 'input',
          label: '账户名',
          prop: 'account',
        },
      ],
      searchModel: {
        roleType: '',
        account: '',
      },
      columns: [
        { prop: 'account', label: '账号名', minWidth: '120px' },
        { prop: 'roleName', label: '角色', minWidth: '100px' },
        { prop: 'name', label: '真实姓名', minWidth: '100px' },
        { prop: 'valid', label: '状态', minWidth: '80px' },
        { prop: 'createTime', label: '创建时间', minWidth: '130px' },
        { prop: 'updateTime', label: '更新时间', minWidth: '130px' },
      ],
      tableData: [],
      formData: { ...initFormData },
      isCreate: true,
      isModify: false,
      isModifyPassword: false,
    };
  },
  mixins: [paginationMixin],
  components: {
    formDialog,
  },
  methods: {
    modifyPassword(row) {
      this.isCreate = false;
      this.isModify = false;
      this.isModifyPassword = true;
      this.formData = {
        id: row.id,
      };
      this.$refs.formDialog.dialogVisible = true;
    },
    createAccount() {
      this.isCreate = true;
      this.isModify = false;
      this.isModifyPassword = false;
      this.formData = { ...initFormData };
      this.$refs.formDialog.dialogVisible = true;
    },
    modify(row) {
      this.isCreate = false;
      this.isModify = true;
      this.isModifyPassword = false;
      const {
        account, roleType, name, id,
      } = row;
      this.formData = {
        account,
        roleType,
        name,
        id,
      };
      this.$refs.formDialog.dialogVisible = true;
    },
    async deleteData(row) {
      this.$confirm('确定禁用', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleleAdminUser({
          adminUserId: row.id,
        });
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    async getData() {
      const result = await getAdminUserList(this.searchParams);
      const { list, total } = result;
      this.total = total;
      this.tableData = list.map((item) => {
        const {
          valid, roleType, updateTime, createTime,
        } = item;
        return {
          ...item,
          roleName: ROLE_TYPE[roleType],
          valid: valid === 1 ? '有效' : '无效',
          createTime: this.$formatTime(createTime),
          updateTime: this.$formatTime(updateTime),
        };
      });
    },
  },
  async created() {
    await this.getData();
  },
};
</script>
